
/*-----------------------------------------------File Info------------------------------------------------
** File Name:               usrTask.c  
** Last modified date:      2016.07.22
** Last version:            V0.1
** Description:             
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.07
** author:                  YLY
** Version:                 V0.1
** Descriptions:            任务1 
**--------------------------------------------------------------------------------------------------------
*/

/*************************************************************
* 	include 
* 	头文件	
*************************************************************/ 
#include "osInclude.h"

#include "ctrl.h"
#include "mainTask.h"
#include "key.h"
#include "arithmetic.h"
#include "bootloader.h"
#include "buzz.h"
#include "bspPWM.h"
/* Define task ID to store all variables  */
/* 定义任务ID存储全部变量,在创建时系统会返回当前任务的ID */

uint8_t gMainTaskId;
uint16_t gVoltage[2];
tsCfgAdc gtCfgAdc0 = DEV_ADC0_0;
tsCfgAdc gtCfgAdc1 = DEV_ADC0_1;
static uint8_t gAdcLed0,gAdcLed1;
	

/**
*	function:	
*	作用:	 获取设备工作电压
*	return :	no
*	返回:	
**/
void getVoltage()
{
	uint16_t count;
	uint16_t adcValue;
			
	adcValue = adcSample(&gtCfgAdc0);	//读取一次电压ad值
	gVoltage[0] = (uint16_t)((uint32_t)adcValue*(uint32_t)3300/(uint32_t)4096);/* 电压,单位mV */
	gAdcLed0 = gVoltage[0]*LED_PERIOD/3300;
	
	adcValue = adcSample(&gtCfgAdc1);	//读取一次电压ad值
	gVoltage[1] = (uint16_t)((uint32_t)adcValue*(uint32_t)3300/(uint32_t)4096);/* 电压,单位mV */	
	gAdcLed1 = gVoltage[1]*LED_PERIOD/3300;	
	
	MAIN_DBG("gAdcLed0=%d,gAdcLed1=%d\r\n",gAdcLed0,gAdcLed1);
	
	MAIN_DBG("vol0=%dmV,cut0=%fmA,vol0=%dmV \r\nvol1=%dmV,cut1=%fmA,vol0=%dmV\r\n",\
	gVoltage[0],gVoltage[0]/150.0,gVoltage[0]*250/150,gVoltage[1],gVoltage[1]/150.0,gVoltage[1]*250/150);	
}

/**
*	function: ms定时器
*	作用:	 
*	return :	no
*	返回:	
**/
void msTimer(void)
{
	static uint16_t i=0;
	
	if(++i == LED_PERIOD)
	{
		LED1_ON();
		LED2_ON();	
		i = 0;
	}
	if(i == gAdcLed0)
		LED1_OFF();
	if(i == gAdcLed1)
		LED2_OFF();		
	
}
/**
*	function:	秒定时器
*	作用:	 
*	return :	no
*	返回:	
**/
void secTimer(void)
{
	static uint8_t wdtCount=0;
	static uint8_t adcCount=0;
		
	if(++adcCount == GET_ADC_TIME)	
	{
		adcCount = 0;
		sendMsgToTask(gMainTaskId, MAIN_GET_ADC_MSG, NULL, 0);	
	}
}

/**
*	function:	主任务
*	作用:	 
*	return :	no
*	返回:	
**/
void mainTask(uint8_t taskId, uint8_t msg ,void *pData, uint16_t dataSize)
{
	static bool gDoorMagneticOpenSta = false;	//门磁状态
	static bool voltageRecover=true;	/* 标识电压恢复 */
	static bool bDoorClose = true;
	tsUserData *ptUserData;	
	uint16_t pkLen,daLen;
	uint8_t data;
	int	i;
	uint8_t *sendBuf;	
	uint16_t adcValue;
	uint32_t bit32;
	uint32_t tmp32;
	
	switch(msg)
	{
		/* 系统初始化 */
		case TASK_INIT:	
		{		
			adcInit(&gtCfgAdc0);	
			buzzStart(500,1);
			createTimer(1,msTimer);	
			createTimer(1000,secTimer);	
			keyInit();
			sendMsgToTask(gMainTaskId, MAIN_GET_ADC_MSG, NULL, 0);
		}break;

		/* 按键 */
		case MAIN_KEY_MSG:
		{
			printf("%d",(uint32_t)pData);
			osFree(sendBuf);			
		}break;

		case MAIN_MODULE_UART0_RECEIVE_DATA:
		{
			osFree(pData);
		}break;		
	}						
}





#if DEFAULT_IDLE_TASK == 0
void idleTask(uint8_t taskId, uint8_t msg, void *pData, uint16_t dataSize)
{
	static uint32_t uiIdleTimes = 0;
	switch(msg)
	{
		case TASK_INIT:	/* 系统初始化 */
		break;
		default:
			if(++ uiIdleTimes == 0xffffffff)
				uiIdleTimes = 0;
			if(uiIdleTimes % 0x80000 == 0)
				LED_TOGGLE();
		break;
	}
}                   
#endif

